package com.golden.order02.service;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.golden.order02.domain.DocumentDO;
import com.golden.order02.domain.GroupDO;
import com.golden.order02.domain.OpenOrderDO;
import com.golden.order02.domain.OrderBasisDO;
import com.golden.order02.domain.OrderCashierDO;
import com.golden.order02.domain.OrderDO;
import com.golden.order02.domain.OrderHisItemDO;
import com.golden.order02.domain.OrderHisSalesDO;
import com.golden.order02.domain.OrderHisSessionDO;
import com.golden.order02.domain.OrderListDO;
import com.golden.order02.domain.OrderProductItemDO;
import com.golden.order02.domain.OrderSalePerformanceDO;
import com.golden.order02.domain.OrderSessionDO;
import com.golden.order02.domain.PriceDO;
import com.golden.order02.domain.ResultDO;
import com.golden.order02.query.OOrderExample;
import com.golden.order02.query.OrderHisItemExample;
import com.golden.order02.query.OrderHisSalesExample;
import com.golden.order02.query.OrderHisSessionExample;
import com.golden.order02.query.OrderProductItemExample;
import com.golden.order02.query.OrderSessionExample;
import com.golden.order02.queryDO.OrderQueryDO;
import com.golden.order02.util.WordTempleteUtils;
import com.jingyanzi.finance.domain.CashBillDO;
import com.jingyanzi.finance.factory.FinanceFactory;
import com.jingyanzi.finance.service.IBillService;
import com.jingyanzi.member.domain.CustomerApiDO;
import com.jingyanzi.member.factory.MemberFactory;
import com.jingyanzi.member.service.ICustomerClient;
import com.jingyanzi.product02.domain.PackageGroupDO;
import com.jingyanzi.product02.domain.PhotoPackageDO;
import com.jingyanzi.product02.domain.PhotoSessionDO;
import com.jingyanzi.product02.domain.ProductDO;
import com.jingyanzi.product02.domain.ProductImageDO;
import com.jingyanzi.product02.domain.SaleCityPriceDO;
import com.jingyanzi.product02.domain.SceneDO;
import com.jingyanzi.product02.domain.ServiceDO;
import com.jingyanzi.product02.factory.Product02Factory;
import com.jingyanzi.product02.query.ProductImageQueryDO;
import com.jingyanzi.product02.service.ProductClientAPI;

@Service("orderGroupService")
public class OrderGroupServiceImpl implements OrderGroupService {

	//@Value("${pdf.order.source}")
	private String source;
	//@Value("${pdf.order.stencil}")
	private String stencil;
	//@Value("${pdf.order.aims}")
	private String aims;

	//@Value("${pdf.order.document}")
	private String document;

	public OrderGroupServiceImpl() {
		InputStream inStream = this.getClass().getClassLoader().getResourceAsStream("applicationOrder.properties");
    	Properties pro=new Properties();
    	try {
			pro.load(inStream);
			aims=pro.getProperty("pdf.order.aims");
			stencil=pro.getProperty("pdf.order.stencil");
			source=pro.getProperty("pdf.order.source");
			document=pro.getProperty("pdf.order.document");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// TODO Auto-generated constructor stub
	}
	// Product02Factory. getProductClientAPI();
	private ProductClientAPI getProductClientAPI() {
		return Product02Factory.getProductClientAPI();
	}
	
	/*private IChanceService getIChanceService() {
		
		return MemberFactory.getChanceService();
	}*/
	
	public ICustomerClient getICustomerClient() {
		
		return MemberFactory.getCustomerClient();
	}
	// customerCientAPI 这里差客户

	@Autowired
	OrderHisItemService orderHisItemService;

	@Autowired
	OrderHisSessionService orderHisSessionService;

	@Autowired
	OrderHisSalesService orderHisSalesService;

	@Autowired
	OrderService orderService;

	@Autowired
	OrderSalePerformanceService orderSalePerformanceService;

	@Autowired
	OrderSessionService orderSessionService;

	@Autowired
	OrderProductItemService orderProductItemService;

	@Autowired
	IDocx2PdfService iDocx2PdfService;

	@Autowired
	OrderDocumentService orderDocumentService;

	public static IBillService getIBillService() {

		return FinanceFactory.getBillService();
	}

	@Override
	public ResultDO<OrderHisSalesDO> openOrder(Integer customerID, Integer leadID) {
		ResultDO<OrderHisSalesDO> result = new ResultDO<>();
		CustomerApiDO customerInfo = new CustomerApiDO();
		// 通过客户ID 查询 客户基本资料
		try {
			customerInfo = getICustomerClient().getCustomerInfo(customerID, leadID);
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		if (leadID == null) {
			leadID = 0;
		}

		OrderDO order = new OrderDO();
		order.setIsDel(-1);
		order.setOrderNO(UUID.randomUUID().toString().replaceAll("-", "").toLowerCase());

		System.out.println(customerInfo+"----------------cccc");
		// 测试
		order.setCustomerName(customerInfo.getCustomerName());
		order.setCustomerTel(customerInfo.getMobileNumber());
		order.setChannelType(customerInfo.getChannelType());
		order.setCustomerStatus(customerInfo.getStatus());
		order.setBusinessType(customerInfo.getBusinessType());
		
		String str="";
		if(customerInfo.getProvince()!=null) {
			str=str+customerInfo.getProvince();
			if(customerInfo.getCity()!=null) {
				str=str+"/"+customerInfo.getCity();
				if(customerInfo.getDistrict()!=null) {
					str=str+"/"+customerInfo.getDistrict();
				}
			}
		}
		order.setCityCode(str);
		order.setCustomerID(customerID);
		order.setLeadID(leadID);
		ResultDO<OrderDO> add = orderService.add(order);

		OrderDO data = add.getData();

		OrderHisSalesDO bo = new OrderHisSalesDO();
		bo.setOrderID(data.getId());
		bo.setIsDel(-1);
		ResultDO<OrderHisSalesDO> add2 = orderHisSalesService.add(bo);

		result.setData(bo);

		return result;
	}

	@Override
	public ResultDO<OrderHisSalesDO> addPackage(Integer packageID, Integer orderSalesID) {

		ResultDO<OrderHisSalesDO> result = new ResultDO<>();
		try {
			// 删除场馆
			OrderHisSessionExample example = new OrderHisSessionExample();
			example.createCriteria().andOrderSalesIDEqualTo(orderSalesID);
			ResultDO<List<OrderHisSessionDO>> query = orderHisSessionService.query(example);
			if (query.isSuccess()) {
				List<OrderHisSessionDO> data = query.getData();
				for (OrderHisSessionDO d : data) {
					orderHisSessionService.delete(d.getId());
				}
			}
			// 删除商品和服务
			OrderHisItemExample example1 = new OrderHisItemExample();
			example1.createCriteria().andOrderSalesIDEqualTo(orderSalesID);
			ResultDO<List<OrderHisItemDO>> query2 = orderHisItemService.query(example1);
			if (query.isSuccess()) {
				List<OrderHisItemDO> data = query2.getData();
				
				if(data!=null) {
					for (OrderHisItemDO od : data) {
						orderHisItemService.delete(od.getId());
					}
				}
			}

			// 修改销售记录
			PhotoPackageDO package1 = getProductClientAPI().getPackage(packageID);
			OrderHisSalesDO beanDO2 = new OrderHisSalesDO();
			beanDO2.setId(orderSalesID);
			beanDO2.setPackageID(packageID);
			beanDO2.setPackageName(package1.getPackageName());
			beanDO2.setCityCode(package1.getCityCode());
			beanDO2.setTxPrice(package1.getListPrice());
			beanDO2.setJtPrice(new BigDecimal(0));
			beanDO2.setTjPrice(new BigDecimal(0));
			beanDO2.setZpPrice(new BigDecimal(0));
			beanDO2.setTotalPrice(package1.getListPrice());
			beanDO2.setPrice(package1.getListPrice());

			orderHisSalesService.modify(beanDO2);
			result.setData(beanDO2);
			List<PackageGroupDO> gePackGoup = getProductClientAPI().gePackGoup(packageID);

			for (PackageGroupDO pd : gePackGoup) {

				PhotoSessionDO getpackageSession = getProductClientAPI().getpackageSession(pd.getId());
				Integer orderHisSessionID = 0;
				// 添加场馆
				List<SceneDO> scene = getProductClientAPI().showByPackIDScene(pd.getId());

				if (scene == null || scene.size() == 0) {
					SceneDO sd = new SceneDO();
					scene.add(sd);
				}
				for (SceneDO sd : scene) {
					OrderHisSessionDO beanDO1 = new OrderHisSessionDO();
					if (sd.getSceneID() == null || sd.getSceneID() == 0) {
						beanDO1.setNegativeListPrice(new BigDecimal(0));
						beanDO1.setNegativeCost(new BigDecimal(0));
						beanDO1.setoNegativeQuant(0);
						beanDO1.setSceneID(0);
						beanDO1.setoNegativeQuant(0);
						beanDO1.setPrice(new BigDecimal(0));
						beanDO1.setSceneID(0);
					} else {
						SaleCityPriceDO getsaleCityPrice = getProductClientAPI()
								.getsaleCityPrice(package1.getCityCode().replaceAll("/", "_"), sd.getCategoryID());
						beanDO1.setNegativeListPrice(
								sd.getNegativeListPrice().multiply(getsaleCityPrice.getPriceRate()));
						beanDO1.setNegativeCost(sd.getNegativeCost().multiply(getsaleCityPrice.getCostRate()));
						beanDO1.setoNegativeQuant(sd.getNegativeNumBase());
						beanDO1.setSceneID(sd.getId());
						beanDO1.setoNegativeQuant(sd.getNegativeNumBase());
						beanDO1.setPrice(sd.getBaseListPrice());
						beanDO1.setSceneID(sd.getSceneID());
					}
					// negativeNumBase
					beanDO1.setOrderSalesID(orderSalesID);
					beanDO1.setSessionName(pd.getSessionName());
					beanDO1.setaNegativeQuant(0);
					beanDO1.setFreeNegativeQuant(0);
					beanDO1.setCreateTime(new Date());
					beanDO1.setIsAdd(0);
					beanDO1.setpProductID(-1);
					beanDO1.setIsReplace(0);
					beanDO1.setIsFree(0);
					beanDO1.setSort(getpackageSession.getSort());
					ResultDO<OrderHisSessionDO> add = orderHisSessionService.add(beanDO1);
					orderHisSessionID = add.getData().getId();
				}

				// 添加商品
				List<ProductDO> product = getProductClientAPI().showByPackIDProduct(pd.getId());
				for (ProductDO p : product) {
					OrderHisItemDO beanDO = new OrderHisItemDO();
					beanDO.setOrderSalesID(orderSalesID);
					beanDO.setProductID(p.getProductID());
					beanDO.setCategoryID(p.getCategoryID());
					beanDO.setType(1);
					beanDO.setIsAdd(0);
					beanDO.setIsFree(0);
					beanDO.setIsReplace(0);
					beanDO.setoRegisterQuant(p.getPhotoNumBase());
					beanDO.setOrderHisSessionID(orderHisSessionID);
					beanDO.setpProductID(-1);
					beanDO.setoQuant(p.getQuantity());
					beanDO.setaQuant(0);
					this.addProduct(beanDO);
				}
				// 添加服务
				List<ServiceDO> service = getProductClientAPI().showByPackIDService(pd.getId());
				for (ServiceDO p : service) {
					OrderHisItemDO beanDO = new OrderHisItemDO();
					beanDO.setOrderSalesID(orderSalesID);
					beanDO.setProductID(p.getServiceID());
					beanDO.setType(2);
					beanDO.setpProductID(-1);
					beanDO.setIsAdd(0);
					beanDO.setIsFree(0);
					beanDO.setIsReplace(0);
					beanDO.setCategoryID(p.getCategoryID());
					beanDO.setOrderHisSessionID(orderHisSessionID);
					beanDO.setoQuant(p.getQuantity());
					beanDO.setaQuant(0);
					this.addProduct(beanDO);
				}
			}

			/*
			 * if(pd.getSort().equals(1000)) { ResultDO<OrderHisSalesDO> resultDO =
			 * orderHisSalesService.get(orderHisSessionID); OrderHisSalesDO data =
			 * resultDO.getData();
			 * 
			 * 
			 * OrderProductItemExample example2=new OrderProductItemExample();
			 * example2.createCriteria().andOrderIDEqualTo(data.getOrderID()).
			 * andStatusNotEqualTo(0);
			 * 
			 * 
			 * ResultDO<List<OrderProductItemDO>> query3 =
			 * orderProductItemService.query(example2);
			 * 
			 * if(!query.isSuccess()) { throw new Exception(); } List<OrderProductItemDO>
			 * data2 = query3.getData();
			 * 
			 * if(data2!=null&&data2.size()!=0) { for(OrderProductItemDO opi:data2) {
			 * 
			 * OrderHisItemDO beanDO = new OrderHisItemDO();
			 * beanDO.setOrderSalesID(orderSalesID); beanDO.setItemID(opi.getId());
			 * 
			 * 
			 * beanDO.setProductID(opi.getProductID());
			 * beanDO.setCategoryID(opi.getCategoryID()); beanDO.setType(opi.getType());
			 * beanDO.setIsAdd(0); beanDO.setIsFree(opi.getIsFree());
			 * beanDO.setIsReplace(0); beanDO.setoRegisterQuant(p.getPhotoNumBase());
			 * beanDO.setOrderHisSessionID(orderHisSessionID); beanDO.setpProductID(-1);
			 * beanDO.setoQuant(p.getQuantity()); beanDO.setaQuant(0);
			 * this.addProduct(beanDO); } } }
			 */

		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setErrorCode(ResultDO.ORDER_ADD_ERROR);
			result.setErrorMsg(ResultDO.ORDER_ADD_MSG_ERROR);
		}
		return result;
	}

	@Override
	public ResultDO<OrderHisItemDO> addProduct(OrderHisItemDO beanDO) {

		if (beanDO.getpProductID() == null) {
			beanDO.setpProductID(0);
		}
		beanDO.setCreateTime(new Date());

		if (beanDO.getaQuant() == null) {
			beanDO.setaQuant(1);
		}
		if (beanDO.getoQuant() == null) {
			beanDO.setoQuant(0);
		}
		ResultDO<OrderHisSessionDO> resultDO1 = orderHisSessionService.get(beanDO.getOrderHisSessionID());

		beanDO.setOrderSalesID(resultDO1.getData().getOrderSalesID());
		beanDO.setaRegisterQuant(0);

		ResultDO<OrderHisSalesDO> resultDO = orderHisSalesService.get(beanDO.getOrderSalesID());

		OrderHisSalesDO data = resultDO.getData();

		String cityCode = data.getCityCode();

		if (beanDO.getIsFree() == null) {
			beanDO.setIsFree(0);
		}
		if (beanDO.getIsAdd() == null) {
			beanDO.setIsAdd(0);
		}
		if (beanDO.getIsReplace() == null) {
			beanDO.setIsReplace(0);
		}

		if (beanDO.getType() == 1) {
			ProductDO product = getProductClientAPI().getProduct(beanDO.getProductID());
			SaleCityPriceDO getsaleCityPrice = getProductClientAPI().getsaleCityPrice(cityCode.replaceAll("/", "_"),
					product.getCategoryID());
			beanDO.setoRegisterQuant(0);
			beanDO.setPNum(product.getPNum());
			beanDO.setCategoryID(product.getCategoryID());
			beanDO.setPrice(product.getBaseListPrice().multiply(getsaleCityPrice.getPriceRate()));
			beanDO.setCost(product.getBaseCost().multiply(getsaleCityPrice.getCostRate()));
			beanDO.setRegisterPrice(product.getAddPhotoPrice().multiply(getsaleCityPrice.getPriceRate()));
			beanDO.setRegisterCost(product.getAddPhotocost().multiply(getsaleCityPrice.getCostRate()));
		}
		if (beanDO.getType() == 2) {
			ServiceDO product = getProductClientAPI().getService(beanDO.getProductID());
			SaleCityPriceDO getsaleCityPrice = getProductClientAPI().getsaleCityPrice(cityCode.replaceAll("/", "_"),
					product.getCategoryID());
			beanDO.setCategoryID(product.getCategoryID());
			beanDO.setPrice(product.getBaseListPrice().multiply(getsaleCityPrice.getPriceRate()));
			beanDO.setCost(product.getBaseCost().multiply(getsaleCityPrice.getCostRate()));
		}
		ResultDO<OrderHisItemDO> add = orderHisItemService.add(beanDO);
		return add;
	}

	@Override
	public ResultDO<Boolean> replaceProduct(Integer orderHisItemID, Integer type, Integer productID) {

		ResultDO<Boolean> result = new ResultDO<>();

		ResultDO<OrderHisItemDO> resultDO = orderHisItemService.get(orderHisItemID);
		OrderHisItemDO data = resultDO.getData();

		// 原单
		if (data.getpProductID().equals(-1)) {

			// 把以前变化量替换为 原数量 把新的 变化量变为 原数量
			data.setaQuant(-data.getoQuant());
			data.setIsReplace(1);
			ResultDO<OrderHisItemDO> modify = orderHisItemService.modify(data);

			OrderHisItemDO beanDO = new OrderHisItemDO();
			beanDO.setpProductID(data.getId());
			beanDO.setaQuant(data.getoQuant());
			beanDO.setoQuant(0);
			beanDO.setProductID(productID);
			beanDO.setType(type);
			beanDO.setOrderHisSessionID(data.getOrderHisSessionID());
			beanDO.setOrderSalesID(data.getOrderSalesID());
			// 直接添加数据
			ResultDO<OrderHisItemDO> addProduct = this.addProduct(beanDO);

		} else if (data.getpProductID().equals(0)) {
			// 添加新的
			OrderHisItemDO beanDO = new OrderHisItemDO();
			beanDO.setpProductID(0);
			beanDO.setaQuant(1);
			beanDO.setoQuant(0);
			beanDO.setType(type);
			beanDO.setProductID(productID);
			beanDO.setOrderSalesID(data.getOrderSalesID());
			beanDO.setOrderHisSessionID(data.getOrderHisSessionID());
			ResultDO<OrderHisItemDO> addProduct = this.addProduct(beanDO);
			// 删除老的
			orderHisItemService.delete(orderHisItemID);

		} else {

			// 查出最开始的数据 ，需要里面原数量的数据

			OrderHisItemDO beanDO = new OrderHisItemDO();
			beanDO.setpProductID(data.getpProductID());
			beanDO.setaQuant(data.getaQuant());
			beanDO.setoQuant(0);
			beanDO.setType(type);
			beanDO.setProductID(productID);
			beanDO.setOrderSalesID(data.getOrderSalesID());
			beanDO.setOrderHisSessionID(data.getOrderHisSessionID());
			ResultDO<OrderHisItemDO> addProduct = this.addProduct(beanDO);
			// 删除老的
			orderHisItemService.delete(orderHisItemID);
		}
		return result;
	}

	@Override
	public ResultDO<Boolean> updateProduct(OrderHisItemDO beanDO) {

		ResultDO<Boolean> result = new ResultDO<>();

		orderHisItemService.modify(beanDO);

		return result;
	}

	@Override
	public ResultDO<Boolean> updateScene(OrderHisSessionDO beanDO) {

		ResultDO<Boolean> result = new ResultDO<>();

		orderHisSessionService.modify(beanDO);

		return result;
	}

	@Override
	public ResultDO<Boolean> replaceScene(OrderHisSessionDO beanD) {

		ResultDO<Boolean> result = new ResultDO<>();

		ResultDO<OrderHisSessionDO> resultDO = orderHisSessionService.get(beanD.getId());
		OrderHisSessionDO data = resultDO.getData();

		SceneDO scene = getProductClientAPI().getScene(beanD.getSceneID());

		// 查出销售记录ID 获取地区Code
		ResultDO<OrderHisSalesDO> resultDO2 = orderHisSalesService.get(data.getOrderSalesID());
		OrderHisSalesDO data2 = resultDO2.getData();

		// 获取价格系数
		SaleCityPriceDO getsaleCityPrice = getProductClientAPI().getsaleCityPrice(data2.getCityCode(),
				scene.getCategoryID());

		// 原单
		if (data.getpProductID().equals(-1)) {
			data.setIsReplace(1);
			ResultDO<OrderHisSessionDO> modify = orderHisSessionService.modify(data);

			OrderHisSessionDO beanDO1 = new OrderHisSessionDO();
			beanDO1.setOrderSalesID(data.getOrderSalesID());
			beanDO1.setSessionName(data.getSessionName());
			beanDO1.setSceneID(beanD.getSceneID());
			beanDO1.setNegativeListPrice(scene.getNegativeListPrice().multiply(getsaleCityPrice.getPriceRate()));
			beanDO1.setNegativeCost(scene.getNegativeCost().multiply(getsaleCityPrice.getCostRate()));
			beanDO1.setaNegativeQuant(scene.getNegativeNumBase());
			beanDO1.setoNegativeQuant(0);
			beanDO1.setFreeNegativeQuant(0);
			beanDO1.setCreateTime(new Date());
			beanDO1.setIsAdd(0);
			beanDO1.setpProductID(data.getId());
			beanDO1.setIsReplace(0);
			beanDO1.setIsFree(0);
			ResultDO<OrderHisSessionDO> add = orderHisSessionService.add(beanDO1);

		} else if (data.getpProductID().equals(0)) {

			OrderHisSessionDO beanDO1 = new OrderHisSessionDO();
			beanDO1.setOrderSalesID(data.getOrderSalesID());
			beanDO1.setSessionName(data.getSessionName());
			beanDO1.setSceneID(beanD.getSceneID());
			beanDO1.setNegativeListPrice(scene.getNegativeListPrice().multiply(getsaleCityPrice.getPriceRate()));
			beanDO1.setNegativeCost(scene.getNegativeCost().multiply(getsaleCityPrice.getCostRate()));
			beanDO1.setaNegativeQuant(scene.getNegativeNumBase());
			beanDO1.setoNegativeQuant(0);
			beanDO1.setFreeNegativeQuant(0);
			beanDO1.setCreateTime(new Date());
			beanDO1.setIsAdd(0);
			beanDO1.setpProductID(0);
			beanDO1.setIsReplace(0);
			beanDO1.setIsFree(0);
			ResultDO<OrderHisSessionDO> add = orderHisSessionService.add(beanDO1);

			orderHisItemService.delete(beanD.getId());

		} else {
			OrderHisSessionDO beanDO1 = new OrderHisSessionDO();
			beanDO1.setOrderSalesID(data.getOrderSalesID());
			beanDO1.setSessionName(data.getSessionName());
			beanDO1.setSceneID(beanD.getSceneID());
			beanDO1.setNegativeListPrice(scene.getNegativeListPrice().multiply(getsaleCityPrice.getPriceRate()));
			beanDO1.setNegativeCost(scene.getNegativeCost().multiply(getsaleCityPrice.getCostRate()));
			beanDO1.setaNegativeQuant(scene.getNegativeNumBase());
			beanDO1.setoNegativeQuant(0);
			beanDO1.setFreeNegativeQuant(0);
			beanDO1.setCreateTime(new Date());
			beanDO1.setIsAdd(0);
			beanDO1.setpProductID(data.getpProductID());
			beanDO1.setIsReplace(0);
			beanDO1.setIsFree(0);
			ResultDO<OrderHisSessionDO> add = orderHisSessionService.add(beanDO1);
			orderHisItemService.delete(beanD.getId());
		}
		return result;
	}

	@Override
	public ResultDO<PriceDO> getOrderPrice(Integer orderID) {
		ResultDO<PriceDO> result = new ResultDO<>();

		PriceDO price = new PriceDO();

		ResultDO<OrderHisSalesDO> resultDO = orderHisSalesService.get(orderID);
		OrderHisSalesDO data = resultDO.getData();
		price.setTxPrice(data.getTxPrice());

		OrderHisSalesExample example4 = new OrderHisSalesExample();

		example4.createCriteria().andOrderIDEqualTo(data.getOrderID()).andIsDelNotEqualTo(-1);
		ResultDO<List<OrderHisSalesDO>> query33 = orderHisSalesService.query(example4);
		if (query33.isSuccess()) {
			List<OrderHisSalesDO> data2 = query33.getData();
			if (data2 != null && data2.size() != 0) {
				BigDecimal p = new BigDecimal(0);

				for (OrderHisSalesDO dd : data2) {
					p = p.add(dd.getPrice());
				}
				price.setYfPrice(p);
			}
		}

		BigDecimal tjPrice = new BigDecimal(0); // 特价商品
		BigDecimal jxPrice = new BigDecimal(0); // 加选商品
		BigDecimal zsPrice = new BigDecimal(0); // 赠送商品
		BigDecimal hjPrice = new BigDecimal(0); // 合计商品

		OrderHisItemExample example = new OrderHisItemExample();
		example.createCriteria().andOrderSalesIDEqualTo(orderID);

		ResultDO<List<OrderHisItemDO>> query = orderHisItemService.query(example);

		OrderHisSessionExample example1 = new OrderHisSessionExample();
		example1.createCriteria().andOrderSalesIDEqualTo(orderID);

		if (query.isSuccess()) {
			List<OrderHisItemDO> data2 = query.getData();

			for (OrderHisItemDO otd : data2) {
				if (otd.getaQuant() == 0) {
					continue;
				}
				// 赠送
				if (otd.getIsFree() == 1) {
					zsPrice = zsPrice.add(new BigDecimal(otd.getPrice().doubleValue() * otd.getaQuant()));
					if (otd.getType() == 1) {
						zsPrice = zsPrice
								.add(new BigDecimal(otd.getRegisterPrice().doubleValue() * otd.getaRegisterQuant()));
					}
					continue;
				}
				// 加选或者升级
				jxPrice = jxPrice.add(new BigDecimal(otd.getPrice().doubleValue() * otd.getaQuant()));
				if (otd.getType() == 1) {
					jxPrice = jxPrice
							.add(new BigDecimal(otd.getRegisterPrice().doubleValue() * otd.getaRegisterQuant()));
				}
			}
		}
		ResultDO<List<OrderHisSessionDO>> query2 = orderHisSessionService.query(example1);
		if (query2.isSuccess()) {
			List<OrderHisSessionDO> data2 = query2.getData();
			for (OrderHisSessionDO otd : data2) {
				zsPrice = zsPrice
						.add(new BigDecimal(otd.getNegativeListPrice().doubleValue() * otd.getFreeNegativeQuant()));
				jxPrice = jxPrice
						.add(new BigDecimal(otd.getNegativeListPrice().doubleValue() * otd.getaNegativeQuant()));

				// 赠送
				if (otd.getIsFree() == 1) {
					zsPrice = zsPrice.add(otd.getPrice());
					continue;
				}
				if (otd.getIsReplace() == 1) {
					jxPrice = jxPrice.subtract(otd.getPrice());
					continue;
				}
				jxPrice = jxPrice.add(otd.getPrice());
			}
		}

		price.setZsPrice(zsPrice);
		price.setJxPrice(jxPrice);
		price.setTjPrice(tjPrice);

		if (jxPrice.doubleValue() < 0) {
			jxPrice = new BigDecimal(0);
		}
		hjPrice = jxPrice.add(price.getTjPrice());

		price.setHjPrice(hjPrice.add(price.getTxPrice()));

		result.setData(price);
		return result;
	}

	@Override
	public ResultDO<List<OrderHisSessionDO>> getGroup(Integer orderHisSalesID) {
		// TODO Auto-generated method stub

		ResultDO<List<OrderHisSessionDO>> result = new ResultDO<>();

		OrderHisSessionExample example = new OrderHisSessionExample();
		example.createCriteria().andOrderSalesIDEqualTo(orderHisSalesID).andIsReplaceEqualTo(0);

		ResultDO<List<OrderHisSessionDO>> query = orderHisSessionService.query(example);

		if (query.isSuccess()) {
			List<OrderHisSessionDO> data = query.getData();

			ListIterator<OrderHisSessionDO> listIterator = data.listIterator();

			BaseServiceImpl<com.golden.order02.domain.SceneDO, SceneDO> bi = new BaseServiceImpl<>();
			com.golden.order02.domain.SceneDO scene1 = new com.golden.order02.domain.SceneDO();

			while (listIterator.hasNext()) {
				OrderHisSessionDO next = listIterator.next();
				if (next.getSceneID() == null || next.getSceneID() == 0) {
					continue;
				}
				SceneDO scene = getProductClientAPI().getScene(next.getSceneID());
				com.golden.order02.domain.SceneDO t = bi.getT(scene, scene1);
				next.setScene(t);
				next.setUnit(scene.getUnit());
				next.setShotRate(scene.getShotRate());
				next.setSceneName(scene.getSceneName());
				next.setShotHours(scene.getShotHours());
				next.setCategoryName(scene.getCategoryName());
				next.setCategoryID(scene.getCategoryID());
				next.setClassCode(scene.getClassCode());
				if(next.getPlanShotDate()!=null) {
					
					try {
						next.setPlanShotDateName(next.getPlanShotDate().getTime()+"");
					} catch (Exception e) {
						// TODO: handle exception
						next.setPlanShotDateName("");
					}
				}else {
					next.setPlanShotDateName(""); 
				}
			}

			result.setData(data);
		}
		return result;
	}

	@Override
	public ResultDO<List<OrderHisItemDO>> getProduct(Integer orderHisSessionID) {

		System.out.println(orderHisSessionID+"----------------------------------");
		ResultDO<List<OrderHisItemDO>> result = new ResultDO<>();

		OrderHisItemExample example = new OrderHisItemExample();

		example.createCriteria().andOrderHisSessionIDEqualTo(orderHisSessionID).andIsReplaceEqualTo(0)
				.andTypeEqualTo(1);

		ResultDO<List<OrderHisItemDO>> query = orderHisItemService.query(example);

		List<OrderHisItemDO> data = query.getData();
		System.out.println(data);
		if(data==null||data.size()==0) {
			result.setData(data);
			return result;
		}
		
		ListIterator<OrderHisItemDO> listIterator = data.listIterator();

		BaseServiceImpl<com.golden.order02.domain.ProductDO, ProductDO> bi = new BaseServiceImpl<>();
		com.golden.order02.domain.ProductDO scene1 = new com.golden.order02.domain.ProductDO();

		while (listIterator.hasNext()) {
			OrderHisItemDO next = listIterator.next();

			ProductDO product = getProductClientAPI().getProduct(next.getProductID());

			com.golden.order02.domain.ProductDO t = bi.getT(product, scene1);
			next.setProduct(t);

			next.setProductName(product.getProductName());
			next.setCategoryID(product.getCategoryID());
			next.setCategoryName(product.getCategoryName());
			next.setPNum(product.getPNum());
			next.setStatus(product.getStatus());
			next.setUnit(product.getUnit());
			next.setPArea(product.getPArea());
			next.setCount(next.getoQuant() + next.getaQuant());
			next.setNum(next.getaRegisterQuant() + next.getoRegisterQuant());
		}

		result.setData(data);

		return result;
	}

	@Override
	public ResultDO<List<OrderHisItemDO>> getService(Integer orderHisSessionID) {

		ResultDO<List<OrderHisItemDO>> result = new ResultDO<>();
		OrderHisItemExample example = new OrderHisItemExample();

		example.createCriteria().andOrderHisSessionIDEqualTo(orderHisSessionID).andIsReplaceEqualTo(0)
				.andTypeEqualTo(2);

		ResultDO<List<OrderHisItemDO>> query = orderHisItemService.query(example);

		List<OrderHisItemDO> data = query.getData();

		System.out.println(data);
		if(data==null||data.size()==0) {
			result.setData(data);
			return result;
		}
		ListIterator<OrderHisItemDO> listIterator = data.listIterator();

		BaseServiceImpl<com.golden.order02.domain.ServiceDO, ServiceDO> bi = new BaseServiceImpl<>();
		com.golden.order02.domain.ServiceDO scene1 = new com.golden.order02.domain.ServiceDO();

		while (listIterator.hasNext()) {
			OrderHisItemDO next = listIterator.next();

			ServiceDO product = getProductClientAPI().getService(next.getProductID());

			com.golden.order02.domain.ServiceDO t = bi.getT(product, scene1);
			next.setService(t);
			next.setCategoryID(product.getCategoryID());
			next.setServiceName(product.getServiceName());
			next.setProductName(product.getServiceName());
			next.setCategoryName(product.getCategoryName());
			next.setStatus(product.getStatus());
			next.setUnit(product.getUnit());
			next.setCount(1);
			next.setNum(0);
		}
		result.setData(data);
		return result;
	}

	@Override
	public ResultDO<Boolean> OpenOrder(OpenOrderDO beanDO) {
		
		ResultDO<Boolean> result = new ResultDO<>();
		result.setSuccess(true);

		OrderBasisDO api = new OrderBasisDO();
		
		try {
			Integer orderSalesID = beanDO.getOrderSalesID();
			if (orderSalesID == null) {
				throw new Exception("orderSalesID为空");
			}
			Integer saleId=0;
			// 先添加销售人员
			List<OrderSalePerformanceDO> sales = beanDO.getSales();
			if (sales != null && sales.size() != 0) {
				for (OrderSalePerformanceDO sale : sales) {
					sale.setOrderSalesID(orderSalesID);
					ResultDO<OrderSalePerformanceDO> add = orderSalePerformanceService.add(sale);
					if (!add.isSuccess()) {
						throw new Exception("添加销售人失败");
					}
					saleId=add.getData().getSaleEmployeeID();
				}
			}
			// 修改销售记录
			ResultDO<PriceDO> orderPrice = this.getOrderPrice(orderSalesID);
			if (!orderPrice.isSuccess()) {
				throw new Exception("获取价格失败");
			}
			PriceDO data = orderPrice.getData();

			ResultDO<OrderHisSalesDO> resultDO = orderHisSalesService.get(orderSalesID);
			if (!resultDO.isSuccess()) {
				throw new Exception("查询销售记录失败");
			}
			OrderHisSalesDO hisSales = resultDO.getData();
			hisSales.setJtPrice(data.getJxPrice());
			hisSales.setTjPrice(data.getTjPrice());
			hisSales.setZpPrice(data.getZsPrice());
			hisSales.setTxPriceDiscount(beanDO.getTxPriceDiscount());
			hisSales.setJtPriceDiscount(beanDO.getJtPriceDiscount());
			hisSales.setOtherDiscount(beanDO.getOtherDiscount());
			hisSales.setOtherDiscomment(beanDO.getOtherDiscomment());
			hisSales.setObject(beanDO.getObject());
			hisSales.setSaleStoreID(beanDO.getSaleStoreID());
			hisSales.setIsDel(0);
			if (beanDO.getSaleType() == null) {
				hisSales.setSaleType(1);
			} else {
				hisSales.setSaleType(beanDO.getSaleType());
			}
			// 总价格
			BigDecimal sum = new BigDecimal(0);
			sum = hisSales.getTxPrice();
			if (hisSales.getJtPrice().doubleValue() > 0) {
				sum = sum.add(hisSales.getJtPrice());
			}
			if (hisSales.getTjPrice().doubleValue() > 0) {
				sum = sum.add(hisSales.getTjPrice());
			}
			hisSales.setTotalPrice(sum);
			// 应付
			BigDecimal yf = sum;
			if (hisSales.getTxPriceDiscount().doubleValue() > 0) {
				yf = yf.subtract(hisSales.getTxPriceDiscount());
			}
			if (hisSales.getJtPriceDiscount().doubleValue() > 0) {
				yf = yf.subtract(hisSales.getJtPriceDiscount());
			}
			if (hisSales.getOtherDiscount().doubleValue() > 0) {
				yf = yf.subtract(hisSales.getOtherDiscount());
			}
			hisSales.setPrice(yf);
			ResultDO<OrderHisSalesDO> modify = orderHisSalesService.modify(hisSales);
			if (!modify.isSuccess()) {
				throw new Exception("销售记录修改失败");
			}

			// 修改订单
			hisSales.getPackageID();
			PhotoPackageDO package1 = getProductClientAPI().getPackage(hisSales.getPackageID());
			OrderDO order = new OrderDO();
			order.setId(hisSales.getOrderID());
			order.setPackageName(hisSales.getPackageName());
			order.setProductCategoryID(package1.getCategoryID());
			order.setCityCode(package1.getCityCode());
			order.setRemark(beanDO.getRemark());
			order.setIsDel(0);
			ResultDO<OrderDO> modify2 = orderService.modify(order);
			if (!modify2.isSuccess()) {
				throw new Exception("修改订单表失败");
			}

			// 添加session 历史
			// OrderHisSessionExample example =new OrderHisSessionExample();
			// example.createCriteria().andOrderSalesIDEqualTo(orderSalesID);
			ResultDO<List<OrderHisSessionDO>> query = this.getGroup(orderSalesID);
			// ResultDO<List<OrderHisSessionDO>> query =
			// orderHisSessionService.query(example);
			if (!query.isSuccess()) {
				throw new Exception("查询session失败");
			}
			List<OrderHisSessionDO> data2 = query.getData();
			if (data2 != null && data2.size() != 0) {
				for (OrderHisSessionDO o : data2) {
					// 如果被替换了 直接跳过 不生成
					if (o.getIsFree() == 1) {
						continue;
					}
					OrderSessionDO os = new OrderSessionDO();
					os.setSessionName(o.getSessionName());
					os.setOrderID(order.getId());
					os.setSceneID(o.getSceneID());
					os.setSiteID(o.getSiteID());
					os.setPlanShotDate(o.getPlanShotDate());
					os.setRemark(o.getRemark());
					os.setSort(o.getSort());
					os.setServiceState(0);
					os.setNegativeQuant(o.getaNegativeQuant());
					os.setCreateTime(new Date());
					os.setModifiTime(new Date());
					os.setIsDel(0);
					ResultDO<OrderSessionDO> add = orderSessionService.add(os);
					if (!add.isSuccess()) {
						throw new Exception("添加session 历史出错");
					}
					OrderSessionDO data3 = add.getData();
					o.setSessionID(data3.getId());
					o.setModifiTime(new Date());
					ResultDO<OrderHisSessionDO> modify3 = orderHisSessionService.modify(o);
					if (!modify3.isSuccess()) {
						throw new Exception("修改session 出错1");
					}

					if (o.getpProductID() != null && o.getpProductID() != 0 && o.getpProductID() != -1) {
						OrderHisSessionDO data4 = new OrderHisSessionDO();
						data4.setModifiTime(new Date());
						data4.setId(o.getpProductID());
						data4.setSessionID(data3.getId());
						ResultDO<OrderHisSessionDO> modify4 = orderHisSessionService.modify(data4);
						if (!modify4.isSuccess()) {
							throw new Exception("修改session 出错2");
						}
					}


					ResultDO<List<OrderHisItemDO>> query2 = this.getProduct(o.getId());
					ResultDO<List<OrderHisItemDO>> service = this.getService(o.getId());
					// ResultDO<List<OrderHisItemDO>> query2 = orderHisItemService.query(example3);
					if (!query2.isSuccess()) {
						throw new Exception("查询hisTtem失败");
					}
					List<OrderHisItemDO> data5 = query2.getData();

					List<OrderHisItemDO> data4 = service.getData();

					if (data4 != null && data4.size() != 0) {
						data5.addAll(data4);
					}
					if (data5 != null && data5.size() != 0) {
						for (OrderHisItemDO oo : data5) {
							// 被替换的产品应该跳过
							if (oo.getIsFree() == 1) {
								continue;
							}
							// 合计为0的时候跳过
							if (oo.getoQuant() == -oo.getaQuant()) {
								continue;
							}
							OrderProductItemDO ps = new OrderProductItemDO();
							ps.setSessionID(data3.getId());
							ps.setOrderID(order.getId());
							ps.setProductID(oo.getProductID());
							ps.setCategoryID(oo.getCategoryID());
							if (oo.getType() == 1) {
								ps.setName(oo.getProductName());
							}
							if (oo.getType() == 2) {
								ps.setName(oo.getServiceName());
							}
							ps.setQuantity((oo.getoQuant() == null ? 0 : oo.getoQuant())
									+ (oo.getaQuant() == null ? 0 : oo.getaQuant()));
							ps.setRemark(oo.getRemark());
							ps.setPNum(oo.getPNum());
							ps.setRegisterNum((oo.getoRegisterQuant() == null ? 0 : oo.getoRegisterQuant())
									+ (oo.getaRegisterQuant() == null ? 0 : oo.getaRegisterQuant()));
							ps.setStatus(0);
							ps.setIsDel(0);
							ps.setSort(oo.getSort());
							ResultDO<OrderProductItemDO> add2 = orderProductItemService.add(ps);
							if (!add2.isSuccess()) {
								throw new Exception("添加ProductItem失败");
							}
							OrderProductItemDO data6 = add2.getData();
							oo.setItemID(data6.getId());
							oo.setSessionID(data6.getSessionID());
							ResultDO<OrderHisItemDO> modify5 = orderHisItemService.modify(oo);

							if (!modify5.isSuccess()) {
								throw new Exception("修改OrderHisItem失败1");
							}
							if (oo.getpProductID() != null && oo.getpProductID() != 0 && oo.getpProductID() != -1) {
								OrderHisItemDO ddd = new OrderHisItemDO();
								ddd.setId(oo.getpProductID());
								ddd.setItemID(data6.getId());
								ddd.setSessionID(data6.getSessionID());
								ResultDO<OrderHisItemDO> modify4 = orderHisItemService.modify(ddd);
								if (!modify4.isSuccess()) {
									throw new Exception("修改OrderHisItem失败2");
								}
							}
						}
					}
				}
			}

			String addCont = this.addCont(order.getId());

			addCont = addCont.replace(source, document);

			// 添加合同管理
			/*
			 * FileManagementDO fileMangement=new FileManagementDO();
			 * fileMangement.setOrigName("套系拍摄服务合同"); fileMangement.setCategory("contract");
			 * fileMangement.setOrigFile(path); com.jingyanzi.sys.domain.result.ResultDO
			 * add211 = getIFileManagementService().add(fileMangement);
			 * if(add211.isSuccess()) { fileMangement = (FileManagementDO)
			 * add211.getModel(com.jingyanzi.sys.domain.result.ResultDO.FIRST_MODEL_KEY);
			 * }else { logger.error(ResultDO.ORDER_ADD_MSG_ERROR+"文件生成出错");
			 * result.setErrorMsg(ResultDO.ORDER_ADD_MSG_ERROR+"文件生成出错");
			 * result.setErrorCode(ResultDO.ORDER_ADD_ERROR); throw new Exception(); }
			 * path=fileMangement.getUrl();
			 */
			
			ResultDO<OrderDO> resultdd = orderService.get(order.getId());
			if(!resultdd.isSuccess()) {
				throw new Exception();
			}
			OrderDO data3 = resultdd.getData();
			// 账单
			CashBillDO domain = new CashBillDO();
			// domain.setAuthor(author);
			domain.setBillNO(data3.getOrderNO());
			domain.setOrderNO(data3.getOrderNO());
			domain.setOrderId(order.getId());
			domain.setSalesLeadId(orderSalesID);
			domain.setCustomId(data3.getCustomerID());
			domain.setStoreId(beanDO.getSaleStoreID());
			domain.setSaleId(saleId);
			domain.setBillType(1);
			domain.setAmount(yf);
			com.jingyanzi.finance.result.ResultDO adddddd = getIBillService().add(domain);
			if (!adddddd.isSuccess()) {
				result.setErrorMsg(ResultDO.ORDER_ADD_MSG_ERROR + "添加账单失败");
				result.setErrorCode(ResultDO.ORDER_ADD_ERROR);
				throw new Exception();
			}
			
			DocumentDO compact = new DocumentDO();
			compact.setFileUrl(addCont);
			compact.setDocumentNo(UUID.randomUUID().toString());
			compact.setOrderSalesID(orderSalesID);
			compact.setOrderID(order.getId());
			compact.setType(0);
			compact.setFileName("套系拍摄服务合同");
			compact.setStatus(0);
			orderDocumentService.add(compact);
		} catch (Exception e) {
			result.setSuccess(false);
			e.printStackTrace();
			// TODO: handle exception
		}

		return result;
	}


	@Override
	public ResultDO<Boolean> OpenOrderUpgrade(OpenOrderDO beanDO) {
		ResultDO<Boolean> result = new ResultDO<>();

		try {

			Integer orderSalesID = beanDO.getOrderSalesID();
			if (orderSalesID == null) {
				throw new Exception("orderSalesID为空");
			}
			// 先添加销售人员
			List<OrderSalePerformanceDO> sales = beanDO.getSales();
			if (sales != null && sales.size() != 0) {
				for (OrderSalePerformanceDO sale : sales) {
					sale.setOrderSalesID(orderSalesID);
					ResultDO<OrderSalePerformanceDO> add = orderSalePerformanceService.add(sale);
					if (!add.isSuccess()) {
						throw new Exception("添加销售人失败");
					}
				}
			}
			// 修改销售记录
			ResultDO<PriceDO> orderPrice = this.getOrderPrice(orderSalesID);
			if (!orderPrice.isSuccess()) {
				throw new Exception("获取价格失败");
			}
			PriceDO data = orderPrice.getData();

			ResultDO<OrderHisSalesDO> resultDO = orderHisSalesService.get(orderSalesID);
			if (!resultDO.isSuccess()) {
				throw new Exception("查询销售记录失败");
			}
			OrderHisSalesDO hisSales = resultDO.getData();
			hisSales.setJtPrice(data.getJxPrice());
			hisSales.setTjPrice(data.getTjPrice());
			hisSales.setZpPrice(data.getZsPrice());
			hisSales.setTxPriceDiscount(beanDO.getTxPriceDiscount());
			hisSales.setJtPriceDiscount(beanDO.getJtPriceDiscount());
			hisSales.setOtherDiscount(beanDO.getOtherDiscount());
			hisSales.setOtherDiscomment(beanDO.getOtherDiscomment());
			hisSales.setObject(beanDO.getObject());
			hisSales.setSaleStoreID(beanDO.getSaleStoreID());
			hisSales.setIsDel(0);
			if (beanDO.getSaleType() == null) {
				hisSales.setSaleType(1);
			} else {
				hisSales.setSaleType(beanDO.getSaleType());
			}
			// 总价格
			BigDecimal sum = new BigDecimal(0);
			sum = hisSales.getTxPrice();
			if (hisSales.getJtPrice().doubleValue() > 0) {
				sum = sum.add(hisSales.getJtPrice());
			}
			if (hisSales.getTjPrice().doubleValue() > 0) {
				sum = sum.add(hisSales.getTjPrice());
			}
			hisSales.setTotalPrice(sum);

			hisSales.setPaidBefore(data.getYfPrice());

			// 应付
			BigDecimal yf = sum;
			if (hisSales.getTxPriceDiscount().doubleValue() > 0) {
				yf = yf.subtract(hisSales.getTxPriceDiscount());
			}
			if (hisSales.getJtPriceDiscount().doubleValue() > 0) {
				yf = yf.subtract(hisSales.getJtPriceDiscount());
			}
			if (hisSales.getOtherDiscount().doubleValue() > 0) {
				yf = yf.subtract(hisSales.getOtherDiscount());
			}
			yf = yf.subtract(data.getYfPrice());

			hisSales.setPrice(yf);
			ResultDO<OrderHisSalesDO> modify = orderHisSalesService.modify(hisSales);
			if (!modify.isSuccess()) {
				throw new Exception("销售记录修改失败");
			}

			// 删除以前的销售记录

			OrderHisSalesExample example = new OrderHisSalesExample();
			example.createCriteria().andOrderIDEqualTo(hisSales.getOrderID()).andIsDelEqualTo(0);
			ResultDO<List<OrderHisSalesDO>> queryee = orderHisSalesService.query(example);
			if (queryee.isSuccess()) {

				OrderHisSalesDO orderHisSalesDO = queryee.getData().get(0);
				orderHisSalesDO.setIsDel(1);
				ResultDO<OrderHisSalesDO> modify2 = orderHisSalesService.modify(orderHisSalesDO);
				if (!modify2.isSuccess()) {
					throw new Exception("删除以前销售记录失败");
				}

			} else {
				throw new Exception("查询销售记录失败");
			}

			// 删除以前所有商品

			OrderProductItemExample example2 = new OrderProductItemExample();
			example2.createCriteria().andOrderIDEqualTo(hisSales.getOrderID()).andIsDelEqualTo(0);
			ResultDO<List<OrderProductItemDO>> queryf = orderProductItemService.query(example2);
			if (queryf.isSuccess()) {
				List<OrderProductItemDO> data2 = queryf.getData();
				for (OrderProductItemDO oi : data2) {
					oi.setIsDel(1);
					oi.setModifiTime(new Date());
					ResultDO<OrderProductItemDO> modify2 = orderProductItemService.modify(oi);
					if (!modify2.isSuccess()) {
						throw new Exception("删除商品失败");
					}
				}
			} else {
				throw new Exception("查询结果商品失败");
			}

			// 删除所有商品
			OrderSessionExample example3 = new OrderSessionExample();
			example3.createCriteria().andOrderIDEqualTo(hisSales.getOrderID()).andIsDelEqualTo(0);
			ResultDO<List<OrderSessionDO>> queryd = orderSessionService.query(example3);
			if (queryd.isSuccess()) {

				List<OrderSessionDO> data2 = queryd.getData();
				for (OrderSessionDO od : data2) {
					od.setIsDel(1);
					od.setModifiTime(new Date());
					ResultDO<OrderSessionDO> modify2 = orderSessionService.modify(od);
					if (!modify2.isSuccess()) {
						throw new Exception("删除分组失败");
					}
				}
			} else {
				throw new Exception("查询商品失败");
			}

			// 修改订单
			hisSales.getPackageID();
			PhotoPackageDO package1 = getProductClientAPI().getPackage(hisSales.getPackageID());
			OrderDO order = new OrderDO();
			order.setId(hisSales.getOrderID());
			order.setPackageName(hisSales.getPackageName());
			order.setProductCategoryID(package1.getCategoryID());
			order.setCityCode(package1.getCityCode());
			order.setRemark(beanDO.getRemark());
			order.setIsDel(0);
			order.setIsUpgrade(1);
			ResultDO<OrderDO> modify2 = orderService.modify(order);
			if (!modify2.isSuccess()) {
				throw new Exception("修改订单表失败");
			}

			// 添加session 历史
			// OrderHisSessionExample example =new OrderHisSessionExample();
			// example.createCriteria().andOrderSalesIDEqualTo(orderSalesID);
			ResultDO<List<OrderHisSessionDO>> query = this.getGroup(orderSalesID);
			// ResultDO<List<OrderHisSessionDO>> query =
			// orderHisSessionService.query(example);
			if (!query.isSuccess()) {
				throw new Exception("查询session失败");
			}
			List<OrderHisSessionDO> data2 = query.getData();
			if (data2 != null && data2.size() != 0) {
				for (OrderHisSessionDO o : data2) {
					// 如果被替换了 直接跳过 不生成
					if (o.getIsFree() == 1) {
						continue;
					}
					OrderSessionDO os = new OrderSessionDO();
					os.setSessionName(o.getSessionName());
					os.setOrderID(order.getId());
					os.setSceneID(o.getSceneID());
					os.setSiteID(o.getSiteID());
					os.setPlanShotDate(o.getPlanShotDate());
					os.setRemark(o.getRemark());
					os.setSort(o.getSort());
					os.setServiceState(0);
					os.setNegativeQuant(o.getaNegativeQuant());
					os.setCreateTime(new Date());
					os.setModifiTime(new Date());
					os.setIsDel(0);
					ResultDO<OrderSessionDO> add = orderSessionService.add(os);
					if (!add.isSuccess()) {
						throw new Exception("添加session 历史出错");
					}
					OrderSessionDO data3 = add.getData();
					o.setSessionID(data3.getId());
					o.setModifiTime(new Date());
					ResultDO<OrderHisSessionDO> modify3 = orderHisSessionService.modify(o);
					if (!modify3.isSuccess()) {
						throw new Exception("修改session 出错1");
					}

					if (o.getpProductID() != null && o.getpProductID() != 0 && o.getpProductID() != -1) {
						OrderHisSessionDO data4 = new OrderHisSessionDO();
						data4.setModifiTime(new Date());
						data4.setId(o.getpProductID());
						data4.setSessionID(data3.getId());
						ResultDO<OrderHisSessionDO> modify4 = orderHisSessionService.modify(data4);
						if (!modify4.isSuccess()) {
							throw new Exception("修改session 出错2");
						}
					}

					// OrderHisItemExample example3=new OrderHisItemExample();
					// example3.createCriteria().andOrderSalesIDEqualTo(orderSalesID).andOrderHisSessionIDEqualTo(o.getId());

					ResultDO<List<OrderHisItemDO>> query2 = this.getProduct(o.getId());
					ResultDO<List<OrderHisItemDO>> service = this.getService(o.getId());
					// ResultDO<List<OrderHisItemDO>> query2 = orderHisItemService.query(example3);
					if (!query2.isSuccess()) {
						throw new Exception("查询hisTtem失败");
					}
					List<OrderHisItemDO> data5 = query2.getData();

					List<OrderHisItemDO> data4 = service.getData();

					if (data4 != null && data4.size() != 0) {
						data5.addAll(data4);
					}
					if (data5 != null && data5.size() != 0) {
						for (OrderHisItemDO oo : data5) {
							// 被替换的产品应该跳过
							if (oo.getIsFree() == 1) {
								continue;
							}
							// 合计为0的时候跳过
							if (oo.getoQuant() == -oo.getaQuant()) {
								continue;
							}
							OrderProductItemDO ps = new OrderProductItemDO();
							ps.setSessionID(data3.getId());
							ps.setOrderID(order.getId());
							ps.setProductID(oo.getProductID());
							ps.setCategoryID(oo.getCategoryID());
							if (oo.getType() == 1) {
								ps.setName(oo.getProductName());
							}
							if (oo.getType() == 2) {
								ps.setName(oo.getServiceName());
							}
							ps.setQuantity((oo.getoQuant() == null ? 0 : oo.getoQuant())
									+ (oo.getaQuant() == null ? 0 : oo.getaQuant()));
							ps.setRemark(oo.getRemark());
							ps.setPNum(oo.getPNum());
							ps.setRegisterNum((oo.getoRegisterQuant() == null ? 0 : oo.getoRegisterQuant())
									+ (oo.getaRegisterQuant() == null ? 0 : oo.getaRegisterQuant()));
							ps.setStatus(0);
							ps.setIsDel(0);
							ps.setSort(oo.getSort());
							ResultDO<OrderProductItemDO> add2 = orderProductItemService.add(ps);
							if (!add2.isSuccess()) {
								throw new Exception("添加ProductItem失败");
							}
							OrderProductItemDO data6 = add2.getData();
							oo.setItemID(data6.getId());
							oo.setSessionID(data6.getSessionID());
							ResultDO<OrderHisItemDO> modify5 = orderHisItemService.modify(oo);

							if (!modify5.isSuccess()) {
								throw new Exception("修改OrderHisItem失败1");
							}
							if (oo.getpProductID() != null && oo.getpProductID() != 0 && oo.getpProductID() != -1) {
								OrderHisItemDO ddd = new OrderHisItemDO();
								ddd.setId(oo.getpProductID());
								ddd.setItemID(data6.getId());
								ddd.setSessionID(data6.getSessionID());
								ResultDO<OrderHisItemDO> modify4 = orderHisItemService.modify(ddd);
								if (!modify4.isSuccess()) {
									throw new Exception("修改OrderHisItem失败2");
								}
							}
						}
					}
				}
			}
		} catch (Exception e) {
			result.setSuccess(false);
			e.printStackTrace();
			// TODO: handle exception
		}

		return result;
	}

	@Override
	public ResultDO<List<OrderListDO>> list(OrderQueryDO queryDO) {

		ResultDO<List<OrderListDO>> result = new ResultDO<>();

		OOrderExample example = new OOrderExample();
		example.createCriteria().andIsDelEqualTo(0);

		ResultDO<List<OrderDO>> query = orderService.query(example);
		if (query.isSuccess()) {
			List<OrderDO> data = query.getData();
			if (data != null && data.size() != 0) {

				BaseServiceImpl<OrderListDO, OrderDO> bi = new BaseServiceImpl<>();
				OrderListDO dd = new OrderListDO();
				List<OrderListDO> tList = bi.getTList(data, dd);

				ListIterator<OrderListDO> listIterator = tList.listIterator();
				while (listIterator.hasNext()) {

					OrderListDO next = listIterator.next();

					OrderHisSalesExample example1 = new OrderHisSalesExample();
					example1.createCriteria().andOrderIDEqualTo(next.getId()).andIsDelNotEqualTo(-1);

					ResultDO<List<OrderHisSalesDO>> query2 = orderHisSalesService.query(example1);

					BigDecimal price = new BigDecimal(0);

					for (OrderHisSalesDO os : query2.getData()) {
						price = price.add(os.getPrice());
						if(os.getIsDel()==0) {
							ProductImageQueryDO   querydd=new ProductImageQueryDO();
							querydd.setType(4);
							querydd.setProductID(os.getPackageID());
							com.jingyanzi.product02.result.ResultDO query3 = Product02Factory.getProductImageService().query(querydd);
							if(query3.isSuccess()) {
								List<ProductImageDO> tt=(List<ProductImageDO>) query3.getModel(com.jingyanzi.product02.result.ResultDO.FIRST_MODEL_KEY);
								if(tt!=null&&tt.size()!=0) {
									next.setImgaeUrl(tt.get(0).getUrl());
								}
							}
						}
					}
					next.setOrderSumPrice(price);
					next.setYprice(new BigDecimal(0));
					
					if(next.getCreateTime()!=null) {
						next.setModifitimeTimeStamp(next.getCreateTime().getTime()+"");
					}
					next.setWprice(price);

					OrderSessionExample example2 = new OrderSessionExample();
					example2.createCriteria().andOrderIDEqualTo(next.getId()).andIsDelNotEqualTo(0)
							.andSortNotEqualTo(1000);

					ResultDO<List<OrderSessionDO>> query3 = orderSessionService.query(example2);

					next.setPsNum(query3.getData().size());
				}

				result.setData(tList);
			}
		}

		return result;
	}

	@Override
	public ResultDO<List<OrderSessionDO>> getOrderSession(Integer orderId) {

		ResultDO<List<OrderSessionDO>> result = new ResultDO<>();
		if (orderId == null) {
			result.setSuccess(false);
			return result;
		}

		OrderSessionExample example = new OrderSessionExample();
		example.createCriteria().andOrderIDEqualTo(orderId).andIsDelNotEqualTo(0).andSortNotEqualTo(1000);

		ResultDO<List<OrderSessionDO>> query = orderSessionService.query(example);

		if (query.isSuccess()) {
			result.setData(query.getData());
		}
		return result;
	}

	@Override
	public ResultDO<OrderDO> upgradeOrder(Integer orderId) {
		ResultDO<OrderDO> result = new ResultDO<>();

		// 通过客户ID 查询 客户基本资料

		ResultDO<OrderDO> resultDO = orderService.get(orderId);
		OrderDO data = resultDO.getData();

		OrderHisSalesDO bo = new OrderHisSalesDO();
		bo.setOrderID(data.getId());
		bo.setIsDel(-1);
		ResultDO<OrderHisSalesDO> add2 = orderHisSalesService.add(bo);

		data.setOrderSalesID(add2.getData().getId());

		OrderHisSalesExample example = new OrderHisSalesExample();
		example.createCriteria().andIsDelEqualTo(0).andOrderIDEqualTo(orderId);
		ResultDO<List<OrderHisSalesDO>> query = orderHisSalesService.query(example);
		Integer id = query.getData().get(0).getPackageID();
		data.setPackageID(id);
		data.setCategoryID(data.getProductCategoryID());
		result.setData(data);

		return result;
	}

	public String addCont(Integer orderID) {
		ResultDO<OrderDO> resultDO = orderService.get(orderID);
		OrderBasisDO api = new OrderBasisDO();
		try {
			OrderDO data = resultDO.getData();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			CustomerApiDO customerInfo =getICustomerClient().getCustomerInfo(data.getCustomerID(), data.getLeadID());
			System.out.println(customerInfo);
			if (!resultDO.isSuccess()) {
				throw new Exception();
			}

			api.setCustomerName(data.getCustomerName());
			api.setTelephone(data.getCustomerTel());
			api.setQq(customerInfo.getQq());
			api.setProvince(customerInfo.getProvinceStr());
			api.setDistrict(customerInfo.getDistrictStr());
			api.setCity(customerInfo.getCityStr());
			api.setAddress(customerInfo.getAddress());
			api.setName(customerInfo.getBusinessFamilyName());
			api.setGenderName(customerInfo.getGenderName());

			if (customerInfo.getBirthday() != null) {
				api.setBirthday(sdf.format(customerInfo.getBirthday()));
			} else {
				api.setBirthday(null);
			}
			OrderHisSalesExample example = new OrderHisSalesExample();
			example.createCriteria().andOrderIDEqualTo(orderID).andIsDelEqualTo(0);
			ResultDO<List<OrderHisSalesDO>> query = orderHisSalesService.query(example);
			if (!query.isSuccess()) {
				throw new Exception();
			}
			List<OrderHisSalesDO> data2 = query.getData();
			OrderHisSalesDO orderHisSalesDO = data2.get(0);
			api.setPrice(orderHisSalesDO.getPrice() + "");
			api.setGivingAmount(orderHisSalesDO.getZpPrice() + "");

			ResultDO<List<OrderHisSessionDO>> query2 = this.getGroup(orderHisSalesDO.getId());
			if (!query2.isSuccess()) {
				throw new Exception();
			}
			List<OrderHisSessionDO> data3 = query2.getData();
			if (data3.size() > 1) {
				api.setCount(data3.size() - 1 + "");
			} else {
				api.setCount(data3.size() + "");
			}

			List<GroupDO> list1 = new ArrayList<>();

			List<OrderHisItemDO> list = new ArrayList<>(); // 为分组商品
			Integer i = 1;
			for (OrderHisSessionDO od : data3) {

				ResultDO<List<OrderHisItemDO>> list22 = this.getService(od.getId());
				List<OrderHisItemDO> list2 = list22.getData();
				ResultDO<List<OrderHisItemDO>> product = this.getProduct(od.getId());
				List<OrderHisItemDO> list3 = product.getData();

				if (od.getSort() != null && od.getSort().equals(1000)) {
					list2.addAll(list3);
					list = list2;
				} else {
					GroupDO gp = new GroupDO();
					gp.setCount(i + "");
					i++;
					gp.setXinji("三星级");
					gp.setSceneName(od.getSceneName());
					gp.setName("cl");
					gp.setDate(od.getPlanShotDate() + "");
					gp.setRemark(od.getRemark());

					gp.setList2(list2);
					gp.setList3(list3);
					list1.add(gp);
				}
			}

			api.setList(list);
			api.setList1(list1);

			api.setDcount("底片树");

			api.setProductName("产品名称");
			api.setYear("1年");

			api.setDate(sdf.format(new Date()));
			String addContract = this.addContract(api);
			return addContract;
		} catch (Exception e) {
			e.printStackTrace();
			e.printStackTrace();
		}

		return null;
	}

	private String addContract(OrderBasisDO api) {

		Map<String, Object> map = new HashMap<>();

		map.put("telephone", api.getTelephone());
		map.put("customerName", api.getCustomerName());
		map.put("qq", api.getQq());
		map.put("province", api.getProvince());
		map.put("district", api.getDistrict());
		map.put("city", api.getCity());
		map.put("address", api.getAddress());
		map.put("name", api.getName());
		map.put("genderName", api.getGenderName());
		map.put("birthday", api.getBirthday());
		map.put("count", api.getCount());
		map.put("Dcount", api.getDcount());
		map.put("price", api.getPrice());
		map.put("productName", api.getProductName());
		map.put("givingAmount", api.getGivingAmount());
		map.put("year", api.getYear());
		map.put("list1", api.getList1());
		map.put("list", api.getList());
		map.put("hedate", api.getDate());
		boolean generate = WordTempleteUtils.generate(source, stencil, aims, map);

		if (generate) {
			String doc2PdfUtil = iDocx2PdfService.doc2PdfUtil(aims);
			return doc2PdfUtil;
		}
		return null;
	}

	@Override
	public ResultDO<OrderHisSalesDO> AddOrder(Integer orderSessionID) {

		ResultDO<OrderHisSalesDO> result = new ResultDO<>();

		try {
			// 先生成订单详情
			ResultDO<OrderSessionDO> resultDO = orderSessionService.get(orderSessionID);
			if (!resultDO.isSuccess()) {
				throw new Exception("查询场次失败");
			}
			OrderSessionDO data = resultDO.getData();

			// 查询订单
			ResultDO<OrderDO> resultDO2 = orderService.get(data.getOrderID());
			if (!resultDO2.isSuccess()) {
				throw new Exception();
			}
			OrderDO data4 = resultDO2.getData();

			OrderHisSalesDO bo = new OrderHisSalesDO();
			bo.setOrderID(data.getOrderID());
			bo.setIsDel(-1);
			bo.setCityCode(data4.getCityCode());
			ResultDO<OrderHisSalesDO> add2 = orderHisSalesService.add(bo);
			if (!add2.isSuccess()) {
				throw new Exception("添加详情失败");
			}
			OrderHisSalesDO data2 = add2.getData();

			// 添加场次
			OrderSessionExample example = new OrderSessionExample();
			example.createCriteria().andIsDelEqualTo(0).andSortEqualTo(1000).andOrderIDEqualTo(data.getOrderID());
			ResultDO<List<OrderSessionDO>> query = orderSessionService.query(example);
			if (!query.isSuccess()) {
				throw new Exception("查询未分组失败");
			}
			List<OrderSessionDO> data3 = query.getData();
			if (data3 == null) {
				data3 = new ArrayList<>();
			}
			data3.add(data);

			for (OrderSessionDO od : data3) {
				OrderHisSessionDO osd = new OrderHisSessionDO();
				OrderHisSessionDO beanDO1 = new OrderHisSessionDO();
				if (osd.getSceneID() == null || osd.getSceneID() == 0) {
					beanDO1.setNegativeListPrice(new BigDecimal(0));
					beanDO1.setNegativeCost(new BigDecimal(0));
					beanDO1.setoNegativeQuant(0);
					beanDO1.setSceneID(0);
					beanDO1.setoNegativeQuant(0);
					beanDO1.setPrice(new BigDecimal(0));
					beanDO1.setSceneID(0);
				} else {
					SceneDO sd = getProductClientAPI().getScene(osd.getSceneID());
					SaleCityPriceDO getsaleCityPrice = getProductClientAPI()
							.getsaleCityPrice(data4.getCityCode().replaceAll("/", "_"), sd.getCategoryID());
					beanDO1.setNegativeListPrice(sd.getNegativeListPrice().multiply(getsaleCityPrice.getPriceRate()));
					beanDO1.setNegativeCost(sd.getNegativeCost().multiply(getsaleCityPrice.getCostRate()));
					beanDO1.setoNegativeQuant(sd.getNegativeNumBase());
					beanDO1.setSceneID(sd.getId());
					beanDO1.setoNegativeQuant(sd.getNegativeNumBase());
					beanDO1.setPrice(sd.getBaseListPrice());
					beanDO1.setSceneID(sd.getSceneID());
				}
				beanDO1.setPlanShotDate(od.getPlanShotDate());
				// negativeNumBase
				beanDO1.setSessionID(od.getId());
				beanDO1.setOrderSalesID(data2.getId());
				beanDO1.setSessionName(od.getSessionName());
				beanDO1.setaNegativeQuant(0);
				beanDO1.setFreeNegativeQuant(0);
				beanDO1.setCreateTime(new Date());
				beanDO1.setIsAdd(0);
				beanDO1.setpProductID(-1);
				beanDO1.setIsReplace(0);
				beanDO1.setIsFree(0);
				beanDO1.setRemark(od.getRemark());
				beanDO1.setSort(od.getSort());
				ResultDO<OrderHisSessionDO> add = orderHisSessionService.add(beanDO1);

				if (!add.isSuccess()) {
					throw new Exception("添加场次失败");
				}
				Integer id = add.getData().getId();

				OrderProductItemExample example1 = new OrderProductItemExample();
				example1.createCriteria().andSessionIDEqualTo(od.getId()).andIsDelEqualTo(0);

				ResultDO<List<OrderProductItemDO>> query2 = orderProductItemService.query(example1);
				if (!query2.isSuccess()) {
					throw new Exception("查询商品历史失败");
				}

				List<OrderProductItemDO> data5 = query2.getData();
				for (OrderProductItemDO ood : data5) {

					OrderHisItemDO beanDO = new OrderHisItemDO();
					beanDO.setOrderSalesID(data2.getId());
					beanDO.setProductID(ood.getProductID());
					beanDO.setCategoryID(ood.getCategoryID());
					beanDO.setType(ood.getType());
					beanDO.setIsAdd(0);
					beanDO.setIsFree(0);
					beanDO.setIsReplace(0);
					beanDO.setoRegisterQuant(ood.getRegisterNum());
					beanDO.setOrderHisSessionID(id);
					beanDO.setpProductID(-1);
					beanDO.setoQuant(ood.getQuantity());
					beanDO.setaQuant(0);
					ResultDO<OrderHisItemDO> addProduct = this.addProduct(beanDO);
					if (!addProduct.isSuccess()) {
						throw new Exception("添加产品失败");
					}
				}
			}
		} catch (Exception e) {
			result.setErrorCode(ResultDO.ORDER_ADD_ERROR);
			result.setErrorMsg(ResultDO.ORDER_ADD_MSG_ERROR);
			e.printStackTrace();
			// TODO: handle exception
		}
		return result;
	}

	
	
	@Override
	public ResultDO<Boolean> updateOrder(OrderCashierDO order) {

		ResultDO<Boolean> result = new ResultDO<>();
		if (order.getOrderId() == null || order.getOrderDetailId() == null) {
			result.setSuccess(false);
			result.setErrorMsg(ResultDO.ORDER_MODIFY_MSG_ERROR+"套件错误");
			result.setErrorCode(ResultDO.ORDER_MODIFY_ERROR);
			return result;
		}

		ResultDO<OrderDO> resultDO = orderService.get(order.getOrderId());

		if (resultDO.isSuccess()) {

			OrderDO model =  resultDO.getData();
			if (model.getAmountPaid() != null&&model.getAmountPaid()!=new BigDecimal(0)) {
				model.setAmountPaid(model.getAmountPaid().add(order.getPrice()));
			} else {
				
				MemberFactory.getCustomerClient().updateToDeal(model.getCustomerID(),model.getLeadID());
				model.setAmountPaid(order.getPrice());
			}

			ResultDO<OrderDO> modify = orderService.modify(model);

			if (!modify.isSuccess()) {
				result.setSuccess(false);
				result.setErrorMsg(ResultDO.ORDER_MODIFY_MSG_ERROR+"修改失败");
				result.setErrorCode(ResultDO.ORDER_MODIFY_ERROR);
				return result;
			}

		} else {
			result.setSuccess(false);
			result.setErrorMsg(ResultDO.ORDER_QUERY_MSG_ERROR+"订单查询失败");
			result.setErrorCode(ResultDO.ORDER_QUERY_ERROR);
			return result;
		}

		ResultDO<OrderHisSalesDO> resultDO2 = orderHisSalesService.get(order.getOrderDetailId());
		if (resultDO2.isSuccess()) {
			OrderHisSalesDO model =resultDO2.getData();
			if (model.getAmountPaid() != null) {
				model.setAmountPaid(model.getAmountPaid().add(order.getPrice()));
			} else {
				model.setAmountPaid(order.getPrice());
			}
			ResultDO<OrderHisSalesDO> modify = orderHisSalesService.modify(model);
			if (!modify.isSuccess()) {
				result.setSuccess(false);
				result.setErrorMsg(ResultDO.ORDER_MODIFY_MSG_ERROR+"订单详情修改失败");
				result.setErrorCode(ResultDO.ORDER_MODIFY_ERROR);
				return result;
			}else {
				
			}
		} else {
			result.setSuccess(false);
			result.setErrorMsg(ResultDO.ORDER_QUERY_MSG_ERROR+"订单详情查询异常");
			result.setErrorCode(ResultDO.ORDER_QUERY_ERROR);
			return result;
		}
		
		
		return result;
	}
	
	@Override
	public ResultDO<Boolean> cancelOrder(Integer id,Integer modifyName) {
		
		ResultDO<Boolean> result=new ResultDO<Boolean>();
		if(id==null||id==0) {
			result.setErrorCode(ResultDO.ORDER_ID_ERROR);
			result.setErrorMsg(ResultDO.ORDER_ID_MSG_ERROR+"取消订单id异常");
			return result;
		}
		
		try {
			//查询该订单
			ResultDO<OrderDO> resultDO = orderService.get(id);
			if(!resultDO.isSuccess()) {
				//查询失败
				result.setErrorCode(ResultDO.ORDER_GET_ERROR);
				result.setErrorMsg(ResultDO.ORDER_GET_MSG_ERROR+"查询订单失败");
				throw new Exception();
			}
			OrderDO model = resultDO.getData();
			if(model.getAmountPaid()!=null&&model.getAmountPaid().intValue()!=0) {
				
				result.setErrorCode(ResultDO.ORDER_GET_ERROR);
				result.setErrorMsg(ResultDO.ORDER_GET_MSG_ERROR+"该订单已付款");
				throw new Exception();
			}
			Date date=new Date();
			model.setModifiTime(date);
			model.setIsDel(1);
			model.setModify(modifyName);
			//删除订单
			ResultDO<OrderDO> modify = orderService.modify(model);
			if(!modify.isSuccess()) {
				result.setErrorCode(ResultDO.ORDER_MODIFY_ERROR);
				result.setErrorMsg(ResultDO.ORDER_MODIFY_MSG_ERROR+"订单修改失败");
				throw new Exception();
			}
			//删除订单合同和文件
			ResultDO<Integer> removeDocument = orderDocumentService.removeDocument(id, modifyName+"");
			if(!removeDocument.isSuccess()) {
				result.setErrorCode(ResultDO.ORDER_DELETE_ERROR);
				result.setErrorMsg(ResultDO.ORDER_DELETE_MSG_ERROR+"删除合同失败");
				throw new Exception();
			}
			
			if(model.getEmployeeID()!=null&&model.getEmployeeID()!=0) {
				//调线索方法
				/*com.jingyanzi.member.domain.result.ResultDO backToChance = getIChanceService().backToChance(model.getEmployeeID(), modifyName);
				if(!backToChance.isSuccess()) {
					result.setErrorCode(ResultDO.ORDER_MODIFY_ERROR);
					result.setErrorMsg(ResultDO.ORDER_MODIFY_MSG_ERROR+"修改线索失败");
					throw new Exception();
				}*/
			}
			//调用账单的方法
			com.jingyanzi.finance.result.ResultDO removeForOrderId = getIBillService().removeForOrderId(id);
			if(!removeForOrderId.isSuccess()) {
				result.setErrorCode(ResultDO.ORDER_DELETE_ERROR);
				result.setErrorMsg(ResultDO.ORDER_DELETE_MSG_ERROR+"删除账单失败");
				throw new Exception();
			}
			
			
		} catch (Exception e) {
			result.setSuccess(false);
		}
		
		return result;
	}
}
